<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use backend\models\EditPassword;
use backend\models\Queues;

/**
 * Site controller
 */
class SiteController extends PublicController
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index', 'editpwd'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }
    
    public function actionEditpwd(){
        $m = new EditPassword();
        if ($data = Yii::$app->request->post()) {
            $m->load($data);
            $m->id = Yii::$app->user->id;
            $result = $m->changePwd();
            return $this->render('editpwd', [
                'model' => $m,
                'result' => $result
            ]);
        }else{
            return $this->render('editpwd', [
                'model' => $m
            ]);
        }
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {

        return $this->render('index', [
            'defaultNumber' => $this->setting('default_call_number_do'),
            'todayQueueNumber' => Queues::todayQueueNumber(date('Y-m-d')),
            'currentQueueNumber' => Queues::currentQueueNumber(date('Y-m-d')),
            'todayCallNumber' => Queues::todayCallNumber(date('Y-m-d')),
            'todayPassNumber' => Queues::todayPassNumber(date('Y-m-d')),
            'todayInNumber' => Queues::todayInNumber(date('Y-m-d'))
        ]);
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
}
